import {
  toast
} from "../../utils/util";

// components/classify/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabIndex: {
      type: Number,
      value: 0
    },
    entry: {
      type: String,
      value: 'index'
    },
    isCheckOut: {
      type: Boolean,
      value: true
    },
    info: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    startDate: '开始时间',
    endDate: '结束时间',
    typeArray: [],
    typeArrayData: [],
    typeIndex: 0,
    height: 0,
    currentIndex: 0,
    radioIndex: 0,
    checkIndex: [],
    check: false, // false单选，true多选
    navArray: [],
    contentList: []
  },
  // 组件生命周期
  lifetimes: {
    ready() {
      this.initDisplay()
      this.initList()
      this.initCheck()
    }
  },
  // 组件页面所在的生命周期
  pageLifetimes: {
    show() {
      this.getRect()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initCheck() {
      this.setData({
        check: this.properties.isCheckOut
      })
    },
    listparams(res) {
      const data = res.data
      const result = []
      data.map((item) => {
        result.push(item.children)
      })
      console.log(result)
      console.log(data)
      this.setData({
        navArray: data,
        contentList: result
      })
      // this.getItemInfo(code, this.itemList.bind(this))
    },
    initList() {
      const code = '001'
      this.findChild(code, this.listparams.bind(this))
    },
    itemList(res) {
      const data = res.data
      this.setData({
        contentList: data
      })
      wx.hideLoading()
    },
    getItemInfo(code, fn) {
      this.findChild(code, fn)
    },
    findChild(code, fn) {
      wx.showLoading({
        title: '加载中',
      })
      app.$api.common.commonFind({
          code
        })
        .then((res) => {
          console.log(res.data)
          fn(res)
          wx.hideLoading()
        }).catch((res) => {
          toast('请稍后再试', 1000)
          wx.hideLoading()
        })
    },
    initDisplay() {
      const result = []
      if (this.properties.tabIndex === 1) {
        const code = '004'
        app.$api.common.commonFind({
          code
        })
        .then((res) => {
          const data = res.data
          data.map((item) => {
            result.push(item.name)
          })
          this.setData({
            typeArray: result,
            typeArrayData: data
          })
        }).catch((e) => {
          toast('请稍后再试', 1000)
        })
      }
      const entry = this.properties.entry
      entry === 'index' ? this.setData({
        check: false
      }) : ''
    },
    bindDateChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        startDate: e.detail.value
      })
    },
    bindDateEndChange(e) {
      this.setData({
        endDate: e.detail.value
      })
    },
    bindDetele() {
      this.setData({
        startDate: '开始时间',
        endDate: '结束时间'
      })
    },
    bindPickerChange(e) {
      this.setData({
        typeIndex: Number(e.detail.value)
      })
    },
    bindCofirm(e) {
      let equipment, code, freedate,type
      const url = this.properties.entry
      const index = this.properties.tabIndex
      const check = this.data.check
      if(this.properties.tabIndex === 1) {
        console.log(this.data.typeArrayData)
        type = this.data.typeArrayData[this.data.typeIndex].code
      } else {
        type = ''
      }
      if (url === 'index') {
        equipment = this.data.contentList[this.data.currentIndex][this.data.radioIndex].name
        code = this.data.contentList[this.data.currentIndex][this.data.radioIndex].code
        freedate = this.getAllDate(this.data.startDate, this.data.endDate)
        console.log(freedate)
        if(freedate === false) {
          return false
        }
      } else {
        if (!check) {
          equipment = this.data.contentList[this.data.currentIndex][this.data.radioIndex].name
          code = this.data.contentList[this.data.currentIndex][this.data.radioIndex].code
        } else {
          const result = []
          const codeResult = []
          const checkIndex = this.data.checkIndex[0]
          this.data.checkIndex.map((item) => {
            result.push(this.data.contentList[this.data.currentIndex][item].name)
            codeResult.push(this.data.contentList[this.data.currentIndex][item].code)
          })
          equipment = result
          code = codeResult
        }
      }
      if (this.properties.entry === 'compile') {
        if (!equipment.length) {
          wx.showModal({
            title: '温馨提示',
            content: '请选择类型',
            success(res) {
              if (res.confirm) {
                return
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          wx.reLaunch({
            url: `/vip/${url}/index?index=${index}&equipment=${equipment}&code=${code}&info=${this.properties.info}`,
          })
        }

      } else {
        if (!equipment.length) {
          wx.showModal({
            title: '温馨提示',
            content: '请选择类型',
            success(res) {
              if (res.confirm) {
                return
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          wx.reLaunch({
            url: `/pages/${url}/index?index=${index}&equipment=${equipment}&code=${code}&freedate=${freedate}&type=${type}`,
          })
        }

      }
    },
    getRect() {
      wx.createSelectorQuery().in(this).select('.top').boundingClientRect(function (rect) {
        return rect
      }).exec((res) => {
        this._initHeight(res[0].height)
      })
    },
    _initHeight(height) {
      const _this = this
      wx.getSystemInfo({
        success(res) {
          _this.setData({
            height: res.windowHeight - height + 'px'
          })
        }
      })
    },
    // 改变currentIndex
    bindChangeIndex(e) {
      const index = e.currentTarget.dataset.index
      const code = this.data.navArray[index].code
      // this.getItemInfo(code, this.itemList.bind(this))
      this.setData({
        currentIndex: index,
        checkIndex: []
      })
    },
    // 改变单选或复选
    bindCheck(e) {
      if (!this.data.check) {
        const index = e.currentTarget.dataset.radio
        this.setData({
          radioIndex: index
        })
        this.bindCofirm()
      }
      if (this.data.check) {
        const index = e.currentTarget.dataset.check
        const checkBox = this.data.checkIndex
        const newIndex = checkBox.indexOf(index)

        if (newIndex > -1) {
          checkBox.splice(newIndex, 1)
          this.setData({
            checkIndex: checkBox
          })
        } else {
          checkBox.push(index)
          this.setData({
            checkIndex: checkBox
          })
        }
      }
    },
    formateDate(time) {
      const month = new Date(time).getMonth() + 1
      const day = new Date(time).getDate()
      const year = new Date(time).getFullYear()
      return `${year}-${month}-${day}`
    },
    getAllDate(start, end) {
      const startDate = new Date()
      const endDate = new Date()
      const result = []
      if (start === '开始时间' || end === '结束时间') {
        return ''
      } else {
        const startNewDate = start.split('-')
        startDate.setUTCFullYear(startNewDate[0], this.zeroSuppression(startNewDate[1]), startNewDate[2])

        const endNewDate = end.split('-')
        endDate.setUTCFullYear(endNewDate[0], this.zeroSuppression(endNewDate[1]), endNewDate[2])
        const startTime = startDate.getTime()
        const endTime = endDate.getTime()
        if (endTime - startTime < 0) {
          wx.showModal({
            title: '温馨提示',
            content: '结束时间不能小于开始时间',
            showCancel: false
          })
          return false
        } else {
          for (let k = startTime; k < endTime; k++) {
            result.push(this.formateDate(new Date(parseInt(k))))
            k = k + 24 * 60 * 60 * 1000
          }
          result.push(this.formateDate(new Date(parseInt(endTime))))
          return result
        }
      }
    },
    zeroSuppression(month) {
      if (Number(month.substring(0, 1)) === 0) {
        return month.substring(1) - 1
      } else {
        return month - 1
      }
    }
  }
})